package tfd.scala.squib.demo;

import tfd.scala.squib._
import tfd.scala.squib.event._

import java.awt._
import java.awt.event._
import java.util.EventObject
import javax.swing._
import javax.swing.event._

object Tester extends Application {
    frame(
  	        attributes(
  	                'defaultCloseOperation -> JFrame.EXIT_ON_CLOSE,
  	                'visible -> true,
                    'size -> new Dimension(200,200),
                    'title -> "Null Layout",
                    'layout -> null
            ),
            contents(
                    button("myid", 
                            attributes('text ->"Press Me",
                                       'location->new Point(40,40),
                                       'size-> new Dimension(150,50))
                             ),
                    button("b2", 
                            attributes('text ->"Me Too!",
                                       'location->new Point(40,100),
                                       'size-> new Dimension(140,30)),
                            events(
                                    'actionperformed -> { ae: ActionEvent => button.id("b2").setText("That Hurt") },
                                    'mousepressed -> { me:MouseEvent => button.id("b2").setText("Pressed Too") }
                            )
                     )
              )
    )
    frame(
            'defaultCloseOperation -> JFrame.EXIT_ON_CLOSE,
            'visible -> true,
            'layout -> flowlayout(),
            'title -> "Slider Test",          
            contents(                 
                     slider("slider", 
                             sliderValueChanged { value:Int => textfield.id("slider_value").setText(value.toString) }),
                     textfield("slider_value", 'columns -> 3,
                     			actionPerformed {  
                     			                   var newValue = 0
                     			                   try {
                                                        newValue = Integer.parseInt(textfield.id("slider_value").getText())
                     			                   } catch {
                                                         case ex:Exception => 
                                                   }
                                                   slider.id("slider").setValue(newValue)
                     			},
                                focusGained { fe: FocusEvent => textfield.id("slider_value").setBackground(Color.yellow) },
                                focusLost { fe: FocusEvent => textfield.id("slider_value").setBackground(Color.white) }
                                 
                     ),
                     label("lbl", 'text->"X", 
                             mouseClicked { me:MouseEvent => label.id("lbl").setText("Clicked") }),
                     button("keyed", 'text->"-----------",
                                     keyPressed { ke:KeyEvent => button.id("keyed").setText("KeyPressed") },
                                     keyReleased { ke:KeyEvent => button.id("keyed").setText("KeyReleased") }
                                     // keyTyped { ke:KeyEvent => button.id("keyed").setText("KeyTyped") }
                     )
            )
                          
     ).pack()
     
     val myIdButton = button.id("myid")  
     button.events(myIdButton, mouseEntered { myIdButton.setText("Thanks") },
                               mouseExited  { myIdButton.setText("Good Bye") },
                               'mousePressed -> { me:MouseEvent => myIdButton.setText("Pressed") },
                               'actionPerformed -> { ae:ActionEvent => myIdButton.setText("Ouch !") }
                    )
}
